package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("region")
@Controller
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        Page<Region> page = regionService.queryPageList(qo);
        model.addAttribute("page",page);
        return "region/list";
    }



    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        if(StringUtils.hasLength(region.getId())){
            regionService.update(region);
        }else{
            regionService.save(region);
        }
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }


    /**
     * 查询关联目的地数据
     * @return
     */
    @RequestMapping("listDest")
    @ResponseBody
    public Object listDest(){
        return destinationService.list();
    }

    /**
     * 根据区域id查找目的地集合
     * @param rid
     * @return
     */
    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> list =destinationService.getDestByRegionId(rid);
        return list;
    }

    /**
     * 改变热门状态
     * @param id
     * @param hot
     * @return
     */
    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id,Integer hot){
        regionService.changeHotValue(id,hot);
        return JsonResult.success();
    }
}
